package com.hq.shop08goods.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hq.commons.ov.Goods;
import com.hq.shop08goods.entity.GoodsEntity;
import com.hq.shop08goods.entity.Stock;
import com.hq.shop08goods.mapper.GoodsMapper;
import com.hq.shop08goods.mapper.StockMapper;
import com.hq.shop08goods.service.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class GoodsServiceImpl extends ServiceImpl< GoodsMapper, GoodsEntity> implements GoodsService {
    @Autowired
    private StockMapper stockMapper;
    @Override
    @Transactional
    public Goods addGoodAndStock(Goods goods) {
//      先插入商品信息
//        把goods中的信息拷贝到商品类中
        GoodsEntity goodsEntity=new GoodsEntity();
//        只要属性名称一样就可以拷贝进去
        BeanUtils.copyProperties(goods,goodsEntity);
        super.save(goodsEntity);
        goods.setId(goodsEntity.getId());
//        插入库存信息
        Stock stock=new Stock();
        stock.setGid(goodsEntity.getId());
        stock.setStock(goods.getGstock());
        stockMapper.insert(stock);
        return goods;
    }
}
